package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.LocalMerchantsReq;
import com.example.xpvideo.entity.req.SupplierMerchantReq;
import com.example.xpvideo.entity.req.UpdateBusinessReq;
import com.example.xpvideo.entity.res.*;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.*;
import com.example.xpvideo.service.PhpLocalMerchnatsService;
import com.example.xpvideo.service.PhpMerchantsService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/8/24 15:06
 * @description:
 */
@Service
public class PhpLocalMerchantsSeviceImpl implements PhpLocalMerchnatsService {
    @Autowired
    private PhpLocalMerchantsMapper phpLocalMerchantsMapper;
    @Autowired
    private LsShopMapper lsShopMapper;
    @Autowired
    private LsShopAdminMapper lsShopAdminMapper;
    @Autowired
    private TimeService timeService;


    @Override
    public ResponseResult<PageResult1<List<PhpLocalMerchantsRes>>> list(LocalMerchantsReq localMerchantsReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String merchantsName = localMerchantsReq.getMerchantsName();
        String account = localMerchantsReq.getAccount();
        Integer stage = localMerchantsReq.getStage();
        String businessName = localMerchantsReq.getBusinessDistrictName();
        Integer allCount = phpLocalMerchantsMapper.allMount();
        Integer newCount = phpLocalMerchantsMapper.newMount();

        PageHelper.startPage(page, size);
        List<PhpLocalMerchantsRes> list =phpLocalMerchantsMapper.LocalMerchantsList (Wrappers.<LsShopAdmin>query()
                .like(!ObjectUtils.isEmpty(merchantsName),"ls.name",merchantsName)
                .eq(!ObjectUtils.isEmpty(account),"account",account)
                .eq(!ObjectUtils.isEmpty(stage),"lsa.disable",stage)
                        .like(!ObjectUtils.isEmpty(businessName),"business_district_name",businessName)
                .eq("type",2));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult1<List<PhpLocalMerchantsRes>> listPageResult = new PageResult1<>(count,allCount,newCount, pages,
                pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }
    @Override
    public ResponseResult<PHPLocalMerchantsDetail> detail(Integer id) throws ErrorException {

        PHPLocalMerchantsDetail phpLocalMerchnatsDetail = phpLocalMerchantsMapper.localMerchantsDetail(Wrappers.<LsShopAdmin>query()
                .eq("lsa.id",id));

        return ResponseResult.responseResult(0, phpLocalMerchnatsDetail);

    }

    @Override
    public ResponseResult<Object> updateBusiness(UpdateBusinessReq updateBusinessReq) throws ErrorException{
        Integer id = updateBusinessReq.getId();
        Integer businessId = updateBusinessReq.getBusinessId();

        LsShop lsShop = new LsShop();
        lsShop.setBusinessId(businessId);
        lsShop.setUpdateTime(timeService.getTime());
        int res = lsShopMapper.update(lsShop,Wrappers.<LsShop>update().eq("merchants_id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"更换/添加商圈成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
